package com.xiaoxin.experience.service.net.model;

import com.xiaoxin.experience.util.SystemUtil;

import java.net.InetAddress;
import java.util.Objects;

/**
 * @author xiaoxin
 */
public class NetworkInfo
{

    private String networkCardName;

    private byte[] hardwareAddress;

    private String ipv4;

    private String ipv6;

    private InetAddress inetAddress;

    public String getNetworkCardName()
    {
        return networkCardName;
    }

    public void setNetworkCardName(String networkCardName)
    {
        this.networkCardName = networkCardName;
    }

    public void setHardwareAddress(byte[] hardwareAddress)
    {
        this.hardwareAddress = hardwareAddress;
    }

    public String getIpv4()
    {
        return ipv4;
    }

    public void setIpv4(String ipv4)
    {
        this.ipv4 = ipv4;
    }

    public String getIpv6()
    {
        return ipv6;
    }

    public void setIpv6(String ipv6)
    {
        this.ipv6 = ipv6;
    }

    public InetAddress getInetAddress()
    {
        return inetAddress;
    }

    public void setInetAddress(InetAddress inetAddress)
    {
        this.inetAddress = inetAddress;
    }

    public String getHardwareAddress()
    {
        if (Objects.isNull(hardwareAddress))
        {
            return null;
        }
        return hardwareAddressAdapt();
    }

    private String hardwareAddressAdapt()
    {
        String divider = "-";
        if (SystemUtil.isLinux())
        {
            divider = ":";
        }
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < hardwareAddress.length; i++)
        {
            sb.append(String.format("%02X%s", hardwareAddress[i], (i < hardwareAddress.length - 1) ? divider : ""));
        }
        return sb.toString();
    }

    @Override
    public String toString()
    {
        return "NetworkInfo{" +
                "networkCardName='" + networkCardName + '\'' +
                ", hardwareAddress=" + getHardwareAddress() +
                ", ipv4='" + ipv4 + '\'' +
                ", ipv6='" + ipv6 + '\'' +
                '}';
    }
}
